
//***************************************************************** */
const app = getApp();

var type = 'add';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    info: null,
    pics: []
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  delImg: function (e) {
    const idx = e.currentTarget.dataset.index;
    var pic= this.data.pics;
    pic.splice(idx, 1);
    this.setData({
      pics: pic
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    const token = extConfig.appid;
    if (options.type) {
      type = options.type;
      wx.setNavigationBarTitle({
        title: '编辑商品',
      })
      var url = 'https://www.58zltc.com/api.php/Goods/getAddForm/type/' + options.type + '/id/' + options.id;
    } else {
      wx.setNavigationBarTitle({
        title: '添加商品',
      })
      var url = 'https://www.58zltc.com/api.php/Goods/getAddForm';
    }
    app.ihttpGet(url, function (res) {
      that.setData({
        input_list: res.data.form,
        add_url: res.data.add_url,
        token: token
      })
    })
    //店铺信息
    app.ihttpGet('https://www.58zltc.com/api.php/Shop/shopInfo/shop_id/' + options.shop_id, function (res) {
      that.setData({
        info: res.data,
        shop_id: options.shop_id,
        order_id: options.order_id
      })
    })
  },
  bindChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].value = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindSwitchChange: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    list.forEach(function (item, idx) {
      if (item.title == data.title) {
        list[idx].checked = e.detail.value;
      }
    })
    this.setData({
      input_list: list
    })
  },
  bindUploadImg: function (e) {
    var data = e.currentTarget.dataset;
    var list = this.data.input_list;
    var that = this;
    var picss = this.data.pics;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '图片上传中……',
        })
        tempFilePaths.forEach(function (item, idx) {
          wx.uploadFile({
            url: 'https://www.58zltc.com/api.php/Index/uploadImage?token=' + that.data.token,
            filePath: item,
            name: 'file',
            success: function (res) {
              var out = JSON.parse(res.data);
              var file_name = out.data.domain;
              if (out.err_code > 0) {
                wx.showToast({
                  title: '上传失败！',
                })
              } else {
                picss.push(file_name);
                that.setData({
                  pics: picss
                })
              }
              wx.hideLoading();
              //do something
            }
          })
        })
      }
    })
  },
  sub: function (e) {
    // console.log(this.data.pics)
    // console.log(e.detail.value)
    let that = this;
    var data = e.detail.value;
    data.pics = this.data.pics;
    data.shop_id = this.data.shop_id;
    data.openid = app.globalData.openid;
    data.order_id = this.data.order_id;
    wx.showLoading({
      title: '正在提交..',
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Shop/addPingjia?token=' + that.data.token,
      data: data,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (e) {
        wx.hideLoading();
        if (e.data.error) {
          wx.showToast({
            title: e.data.msg,
            icon: 'none',
            duration: 1500
          })
        } else {
          wx.showToast({
            title: e.data.msg,
            icon: 'success',
            duration: 1500
          })
        }
        setTimeout(function () {
          wx.navigateBack({
            
          })
        }, 1500)
      }
    })
  },
})